<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="课程名" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['api:course:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['api:course:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['api:course:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['api:course:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="courseList"
      @selection-change="handleSelectionChange"
      @expand-change="handleRowClick"
      ref="refTable"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="expand">
        <template slot-scope="props">
          <div style="padding: 0 200px">
            <el-table v-loading="loading2" :data="courseDetailList">
              <el-table-column
                label="课程标题"
                align="center"
                prop="detailCourseTitle"
              />
              <el-table-column
                label="课程封面"
                align="center"
                prop="detailCourseImage"
                width="100"
              >
                <template slot-scope="scope">
                  <image-preview
                    :src="scope.row.detailCourseImage"
                    :width="50"
                    :height="50"
                  />
                </template>
              </el-table-column>
              <el-table-column
                label="课程视频"
                align="center"
                prop="detailCourseVideo"
              />
              <el-table-column
                label="课程描述"
                align="center"
                prop="detailCourseDetail"
              />
              <el-table-column
                label="观看数量"
                align="center"
                prop="watchNum"
              />
              <el-table-column
                label="是否免费"
                align="center"
                prop="detailChargeOrNot"
              >
                <template slot-scope="scope">
                  <el-tag v-if="scope.row.detailChargeOrNot == 0">免费</el-tag>
                  <el-tag v-if="scope.row.detailChargeOrNot == 1" type="success"
                    >收费</el-tag
                  >
                </template>
              </el-table-column>
              <el-table-column
                label="收费金额"
                align="center"
                prop="detailChargeAmount"
              />
              <el-table-column
                label="课程操作"
                align="center"
                class-name="small-padding fixed-width"
              >
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleCourseUpdate(scope.row)"
                    v-hasPermi="['api:course:edit']"
                    >修改课程</el-button
                  >
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleCourseDelete(scope.row)"
                    v-hasPermi="['api:course:edit']"
                    >删除课程</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <pagination
              v-show="courseDetailTotal > 0"
              :total="courseDetailTotal"
              :page.sync="queryParams2.pageNum"
              :limit.sync="queryParams2.pageSize"
              @pagination="getNewList"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="课程名" align="center" prop="courseName" />
      <el-table-column
        label="课程封面"
        align="center"
        prop="courseImage"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview
            :src="scope.row.courseImage"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="课程描述" align="center" prop="courseDetail" />
      <el-table-column label="课程简介" align="center" prop="courseIntro" />
      <el-table-column label="课程类型" align="center" prop="courseType">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.courseType }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="学习人数" align="center" prop="coursePeople" />
      <el-table-column label="是否免费" align="center" prop="chargeOrNot">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.chargeOrNot == 0">免费</el-tag>
          <el-tag v-if="scope.row.chargeOrNot == 1" type="success">收费</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="收费金额" align="center" prop="chargeAmount" />
      <el-table-column
        label="课程操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCourse(scope.row)"
            v-hasPermi="['api:course:edit']"
            >添加课程</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['api:course:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['api:course:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="120px"
        :inline="true"
      >
        <el-form-item label="课程名" prop="courseName" style="display: block">
          <el-input v-model="form.courseName" placeholder="请输入课程名" />
        </el-form-item>

        <el-form-item label="课程类型" prop="courseType" style="display: block">
          <el-select v-model="form.courseType" placeholder="请选择">
            <el-option
              v-for="item in dict.type.course_type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="课程封面"
          prop="courseImage"
          style="display: block"
        >
          <image-upload v-model="form.courseImage" />
        </el-form-item>
        <el-form-item
          label="课程描述"
          prop="courseDetail"
          style="display: block"
        >
          <el-input
            type="textarea"
            :rows="3"
            v-model="form.courseDetail"
            placeholder="请输入课程描述"
            maxlength="300"
            style="width: 500px"
          />
        </el-form-item>
        <el-form-item
          label="课程简介"
          prop="courseIntro"
          style="display: block"
          maxlength="300"
        >
          <el-input
            type="textarea"
            :rows="4"
            style="width: 500px"
            v-model="form.courseIntro"
            placeholder="请输入课程简介"
          />
        </el-form-item>
        <el-form-item
          label="讲师名称"
          prop="lecturerName"
          style="display: block"
        >
          <div style="display: flex; align-items: center">
            <el-input
              v-model="form.lecturerName"
              placeholder="请输入讲师名称"
              :disabled="true"
            />
            <el-button
              style="margin-left: 20px"
              type="primary"
              @click="drawer = true"
              >选择</el-button
            >
          </div>
        </el-form-item>
        <el-form-item
          label="讲师介绍"
          prop="lecturerDetail"
          style="display: block"
          v-if="form.lecturerName"
        >
          <el-input
            type="textarea"
            :rows="4"
            style="width: 500px"
            v-model="form.lecturerDetail"
            placeholder="请输入讲师介绍"
          />
        </el-form-item>
        <el-form-item
          label="讲师头像"
          prop="lecturerImage"
          style="display: block"
          v-if="form.lecturerName"
        >
          <image-upload v-model="form.lecturerImage" />
        </el-form-item>
        <el-form-item
          label="学习人数"
          prop="coursePeople"
          style="display: block"
        >
          <el-input v-model="form.coursePeople" placeholder="请输入学习人数" />
        </el-form-item>
        <el-form-item
          label="是否免费"
          prop="chargeOrNot"
          style="display: block"
        >
          <!-- <el-input v-model="form.chargeOrNot" placeholder="请输入是否免费" /> -->
          <el-radio-group v-model="form.chargeOrNot">
            <el-radio :label="0">免费</el-radio>
            <el-radio :label="1">收费</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="收费金额"
          prop="chargeAmount"
          style="display: block"
          v-if="form.chargeOrNot == 1"
        >
          <el-input-number
            v-model="form.chargeAmount"
            @change="handleChange"
            :min="1"
            label="请输入收费金额"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :title="title"
      :visible.sync="courseOpen"
      width="50%"
      append-to-body
    >
      <el-form
        ref="form2"
        :model="form2"
        :rules="rules"
        label-width="120px"
        :inline="true"
      >
        <!-- <el-form-item label="课程" prop="dCourseId">
          <el-input v-model="form2.dCourseId" placeholder="请输入课程id" />
        </el-form-item> -->
        <el-form-item
          label="课程标题"
          prop="detailCourseTitle"
          style="display: block"
        >
          <el-input
            v-model="form2.detailCourseTitle"
            placeholder="请输入课程标题"
          />
        </el-form-item>
        <el-form-item
          label="课程封面"
          prop="detailCourseImage"
          style="display: block"
        >
          <image-upload v-model="form2.detailCourseImage" />
        </el-form-item>
        <el-form-item
          label="课程视频"
          prop="detailCourseVideo"
          style="display: block"
        >
          <file-upload v-model="form2.detailCourseVideo" />
        </el-form-item>
        <el-form-item
          label="课程描述"
          prop="detailCourseDetail"
          style="display: block"
        >
          <el-input
            type="textarea"
            :rows="4"
            style="width: 500px"
            v-model="form2.detailCourseDetail"
            placeholder="请输入课程描述"
          />
        </el-form-item>
        <el-form-item label="观看数量" prop="watchNum" style="display: block">
          <el-input v-model="form2.watchNum" placeholder="请输入观看数量" />
        </el-form-item>
        <el-form-item
          label="是否免费"
          prop="detailChargeOrNot"
          style="display: block"
        >
          <!-- <el-input
            v-model="form2.detailChargeOrNot"
            placeholder="请输入是否免费"
          /> -->
          <el-radio-group v-model="form2.detailChargeOrNot">
            <el-radio :label="0">免费</el-radio>
            <el-radio :label="1">收费</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="收费金额"
          prop="detailChargeAmount"
          style="display: block"
          v-if="form2.detailChargeOrNot == 1"
        >
          <el-input-number
            v-model="form2.detailChargeAmount"
            @change="handleChange2"
            :min="1"
            label="请输入收费金额"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-drawer
      title="专家团人选"
      :visible.sync="drawer"
      direction="rtl"
      size="50%"
    >
      <el-table
        v-loading="exoertloading"
        :data="expertTeamList"
        @selection-change="handleSelectionChange2"
        @select="select"
        @select-all="selectAll"
        ref="multipleTable"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="专家头像"
          align="center"
          prop="expertImage"
          width="100"
        >
          <template slot-scope="scope">
            <image-preview
              :src="scope.row.expertImage"
              :width="50"
              :height="50"
            />
          </template>
        </el-table-column>
        <el-table-column label="专家姓名" align="center" prop="expertName" />
        <el-table-column label="专家等级" align="center" prop="expertLevel" />
        <el-table-column label="简介" align="center" prop="expertIntro" />
        <el-table-column
          label="代表课程"
          align="center"
          prop="courseRepresentative"
        />
      </el-table>
      <pagination
        v-show="expertotal > 0"
        :total="expertotal"
        :page.sync="queryParams3.pageNum"
        :limit.sync="queryParams3.pageSize"
        @pagination="getTeamList"
      />
    </el-drawer>
  </div>
</template>

<script>
import {
  listCourse,
  getCourse,
  delCourse,
  addCourse,
  updateCourse,
} from "@/api/course/course";
import {
  listCourseDetail,
  getCourseDetail,
  delCourseDetail,
  addCourseDetail,
  updateCourseDetail,
} from "@/api/course/courseDetail";
import { listExpertTeam } from "@/api/course/expertTeam";

export default {
  name: "Course",
  dicts: ["course_type"],
  data() {
    return {
      expands: [],
      // 遮罩层
      loading: true,
      loading2: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      courseOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseName: null,
        courseImage: null,
        courseDetail: null,
        courseIntro: null,
        courseType: null,
        coursePeople: null,
        chargeOrNot: null,
        chargeAmount: null,
      },
      queryParams2: {
        pageNum: 1,
        pageSize: 10,
      },
      queryParams3: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      form2: {},
      // 表单校验
      rules: {},
      courseDetailList: [],
      drawer: false,
      expertotal: 0,
      exoertloading: false,
      expertTeamList: [],
    };
  },
  created() {
    this.getList();
    this.getTeamList();
    // console.log(this.dict.type.course_type, "course_type");
  },
  methods: {
    /** 查询专家团列表 */
    getTeamList() {
      this.exoertloading = true;
      listExpertTeam(this.queryParams3).then((response) => {
        this.expertTeamList = response.rows;
        this.expertotal = response.total;
        this.exoertloading = false;
      });
    },
    handleSelectionChange2(value) {
      console.log("value", value);
      this.form.lecturerName = value[0].expertName;
      this.form.lecturerId = value[0].expertId;
      this.form.lecturerImage = value[0].expertImage;
      this.form.lecturerDetail = value[0].expertIntro;
    },
    select(selection, row) {
      console.log("selection", selection);
      console.log("row", row);
      if (selection.length > 1) {
        let del_row = selection.shift();
        this.$refs.multipleTable.toggleRowSelection(del_row, false);
      }
    },
    selectAll(selection) {
      // console.log("selection2", selection);
      if (selection.length > 1) {
        selection.length = 1;
      }
    },
    /** 查询课程列表 */
    getList() {
      this.loading = true;
      listCourse(this.queryParams).then((response) => {
        this.courseList = response.rows;
        let arr = this.courseList;
        arr.forEach((item) => {
          item.courseType = this.getValue(
            this.dict.type.course_type,
            item.courseType
          );
        });
        this.courseList = arr;
        this.total = response.total;
        this.loading = false;
      });
    },
    getValue(arr, item) {
      for (let i = 0; i < arr.length; i++) {
        if (item === arr[i].value) {
          return arr[i].label;
        }
      }
    },
    handleChange(e) {
      this.form.chargeAmount = e;
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        courseId: null,
        courseName: null,
        courseImage: null,
        courseDetail: null,
        courseIntro: null,
        courseType: null,
        coursePeople: null,
        chargeOrNot: null,
        chargeAmount: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        lecturerName: null,
        lecturerDetail: null,
        lecturerImage: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.courseId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const courseId = row.courseId || this.ids;
      getCourse(courseId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.courseId != null) {
            updateCourse(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const courseIds = row.courseId || this.ids;
      this.$modal
        .confirm('是否确认删除课程编号为"' + courseIds + '"的数据项？')
        .then(function () {
          return delCourse(courseIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "api/course/export",
        {
          ...this.queryParams,
        },
        `course_${new Date().getTime()}.xlsx`
      );
    },
    handleCourse(e) {
      console.log(e);
      this.title = "添加课程";
      this.reset2();
      this.form2.dCourseId = e.courseId;
      this.courseOpen = true;
    },
    // 取消按钮
    cancel() {
      this.courseOpen = false;
      this.reset();
    },
    // 表单重置
    reset2() {
      this.form2 = {
        dCourseId: null,
        detailCourseImage: null,
        detailCourseVideo: null,
        detailCourseDetail: null,
        watchNum: null,
        detailChargeOrNot: null,
        detailChargeAmount: null,
        delFlag: null,
      };
      this.resetForm("form2");
    },
    handleChange2(e) {
      this.form2.detailChargeAmount = e;
    },
    /** 提交按钮 */
    submitForm2() {
      this.$refs["form2"].validate((valid) => {
        if (valid) {
          if (this.form2.detailId != null) {
            updateCourseDetail(this.form2).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.courseOpen = false;
              this.getNewList(this.form2.dCourseId);
            });
          } else {
            addCourseDetail(this.form2).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.courseOpen = false;
              this.getNewList(this.form2.dCourseId);
            });
          }
        }
      });
    },
    handleRowClick(row, expandedRows) {
      console.log("row", row);
      console.log("expandedRows", expandedRows);
      this.loading2 = true;
      var that = this;
      this.courseDetailList = [];
      if (expandedRows.length > 1) {
        that.expands = [];
        if (row) {
          that.expands.push(row);
        }
        this.$refs.refTable.toggleRowExpansion(expandedRows[0]);
      } else {
        that.expands = [];
      }
      getCourse(row.courseId).then((response) => {
        that.courseDetailList = response.data.courseDetailList;
        this.loading2 = false;
      });
    },
    /** 修改课程操作 */
    handleCourseUpdate(row) {
      console.log("row", row);
      this.reset2();
      const detailId = row.detailId;
      getCourseDetail(detailId).then((response) => {
        console.log(response);
        this.form2 = response.data;
        this.courseOpen = true;
        this.title = "修改课程";
      });
    },
    getNewList(courseId) {
      console.log("1212");
      var that = this;
      getCourse(courseId).then((response) => {
        that.courseDetailList = response.data.courseDetailList;
        that.courseDetailTotal = response.total;
      });
    },
    handleCourseDelete(row) {
      console.log(row);
      var that = this;
      delCourseDetail(row.detailId).then((response) => {
        that.getNewList(row.dCourseId);
        that.$modal.msgSuccess("删除成功");
      });
    },
  },
};
</script>
<style></style>
